/*This file contains the menulist adapter, 
 * it's the link between the interface of the list 
 * and the list of the contacts' name. */
package dk.aau.walkietalkieui.adapter;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import dk.aau.walkietalkieui.R;

public class MenuListAdapter extends BaseAdapter {

	//TODO deal with online and offline contact
	//list of users
	private String[] contactList = new String[]{
			"Broadcast",
			"Katina",
			"Caroline"
	};

	// data to add
	
	public static final int NO_ITEM_SELECTED = -1;
	private Context ctx;
	private int selectedPosition;

	public MenuListAdapter(Context ctx) {
		this.ctx = ctx;
		this.selectedPosition = NO_ITEM_SELECTED;	
	}
	
	@Override
	public int getCount() {
		return contactList.length;
	}

	@Override
	public String getItem(int position) {
		return contactList[position];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v;
		
		if(convertView == null) {
			v = View.inflate(ctx, R.layout.contact_list_row, null);
		} else {
			v = convertView;
		}
		
		//This is the name of the contacts
		((TextView)v.findViewById(R.id.Text)).setText(getItem(position));
		
		//This image is the small image in the list_row
		ImageView image = (ImageView) v.findViewById(R.id.Image);
		
		switch(position) {
		case 0:
			image.setImageDrawable(ctx.getResources().getDrawable(R.drawable.broadcast));
			break;
		default:
			image.setImageDrawable(ctx.getResources().getDrawable(R.drawable.online));
			break;
		}
		
		//This displays the list
		ViewGroup parentLayout = (ViewGroup) v.findViewById(R.id.Parent);
		if(selectedPosition == position) {
			parentLayout.setBackgroundColor(ctx.getResources().getColor(R.color.blue));
		} else {
			parentLayout.setBackgroundColor(ctx.getResources().getColor(R.color.none));
		}
		return v;
	}

	public void setSelectedItem(int position) {
		this.selectedPosition=position;
		notifyDataSetChanged();
	}
}
